﻿#region File Header

/* File Name    : FacebookRequestHandler.cs
 * Description  : This class contains methods to execute a Facebook web request.
 * Log
 * Date             Author              Description
 * 15-Oct-2011      Robin Jome          Created the class
 * 16-Oct-2011      Robin Jome          Removed out parameters from methods and added FacebookException
 * 17-Oct-2011      Robin Jome          Updated ExecutePostRequest().
 */

#endregion

#region Namespaces

using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;

#endregion

namespace FacebookAPI.Base
{
    #region Class

    /// <summary>
    /// This class contains methods to get and post data to and from Facebook
    /// </summary>
    internal class FacebookRequestHandler
    {
        #region Methods

        #region Common Methods

        /// <summary>
        /// This method returns a WebRequest object constructed using the given Uri and values.
        /// </summary>
        /// <param name="url">Represents the Uri to be used to construct the WebRequest.</param>
        /// <param name="values">Represents an array of values to be used to construct the Uri</param>
        /// <returns>Returns a WebRequest object.</returns>
        private WebRequest GetWebRequest(Uri url)
        {
            WebRequest webRequest = WebRequest.Create(url);
            webRequest.Proxy = HttpWebRequest.DefaultWebProxy;
            webRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
            return webRequest;
        }

        #endregion

        #region Get Data

        internal FBObject GetData<FBObject>(Uri url) where FBObject : class
        {
            FBObject responseData = default(FBObject);
            WebRequest webRequest = GetWebRequest(url);
            using (WebResponse webResponse = webRequest.GetResponse())
            {
                if (webResponse != null)
                {
                    DataContractJsonSerializer serializer =
                        new DataContractJsonSerializer(typeof(FBObject), "root");
                    Stream stream = webResponse.GetResponseStream();
                    responseData = (FBObject)serializer.ReadObject(stream);
                }
            }
            return responseData;
        }

        #endregion

        #region Post Data

        internal ReturnObject PostData<FBObject, ReturnObject>(Uri url, string postData) where FBObject : class
        {
            ReturnObject responseData = default(ReturnObject);
            WebRequest webRequest = GetWebRequest(url);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-UrlEncoded";

            byte[] buffer = null;
            if (!string.IsNullOrEmpty(postData))
            {
                buffer = Encoding.UTF8.GetBytes(postData);
                webRequest.ContentLength = buffer.Length;
            }
            using (Stream stream = webRequest.GetRequestStream())
            {
                if (buffer != null)
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
                using (WebResponse webResponse = webRequest.GetResponse())
                {
                    if (webResponse != null)
                    {
                        DataContractJsonSerializer serializer =
                            new DataContractJsonSerializer(typeof(ReturnObject), "root");
                        responseData = (ReturnObject)serializer.ReadObject(webResponse.GetResponseStream());
                    }
                }
            }
            return responseData;
        }

        #endregion

        #endregion
    }

    #endregion
}
